from config import *

def IsLogined():
	return web.ctx.session.IsLogined == 1

class Logout:
	def GET(self):
		web.ctx.session.IsLogined = 0
		web.ctx.session.kill()
		return 'logout'
	
	
class Login:
	def GET(self):
		if IsLogined():
			return 'is logined'
		else:
			return render.login()
			

class VerifyAccount:
	def POST(self):
		input = web.input()
		if input.usercode == '':
			return 0
		myvar = dict(usercode=input.usercode)
		result = db.select(TableRegister, myvar, where="usercode = $usercode")
		#print '*' * 90
		#print len(result)
		if len(result) != 0:
			row = result[0]
			#print '*' * 90
			#print input
			if row['password'] == input.password:
				if input.autologin == 'y':
					web.ctx.session.autologin = 1
				web.ctx.session.IsLogined = 1
				web.ctx.session.userID = row['idregister']
				web.ctx.session.userName = row['username']
				return 1 #verify right
			else:
				return 2 #password error
		else:
			return 0 #no exist account